package com.drakulo.games.ais.ui.component.button;

import org.newdawn.slick.Color;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

import com.drakulo.games.ais.ui.FontHelper;
import com.drakulo.games.ais.ui.ImageManager;
import com.drakulo.games.ais.ui.UIHelper;
import com.drakulo.games.ais.ui.component.Bindable;
import com.drakulo.games.ais.ui.twlbridge.RootPane;

import de.matthiasmann.twl.Label;
import de.matthiasmann.twl.Widget;

public class ImageButton extends Button implements Bindable {
	public static final int IB_DEFAULT_SIZE = 40;
	public static final String DUMMY_IMAGE = "dummy";

	private String imageRef;

	private Label title;

	public ImageButton(String ref, int x, int y, int width, int height) {
		super(x, y, width, height);
		setImageRef(ref);
		createTitle();
	}

	public ImageButton(String ref, int x, int y) {
		super(x, y);
		setImageRef(ref);
		setWidth(IB_DEFAULT_SIZE);
		setHeight(IB_DEFAULT_SIZE);
		createTitle();
	}

	public ImageButton(String ref) {
		super();
		setImageRef(ref);
		setWidth(IB_DEFAULT_SIZE);
		setHeight(IB_DEFAULT_SIZE);
		createTitle();
	}

	@Override
	public void render(Graphics g) throws SlickException {
		super.render(g);
		if (!this.isShown()) {
			return;
		}
		// Rendering the image centered
		final Image img = ImageManager.getGfx(this.imageRef);
		final int imgWidth = img.getWidth();
		final int imgHeight = img.getHeight();

		final int imgX = this.ox + this.width / 2 - imgWidth / 2;
		final int imgY = this.oy + this.height / 2 - imgHeight / 2;

		if (this.state == DISABLED) {
			// If the button is disabled, a filter is applied to the image
			g.drawImage(img, imgX, imgY, new Color(0.5F, 0.5F, 0.5F, 0.5F));
		} else if (this.state == PRESSED) {
			g.drawImage(img, imgX, imgY, new Color(0.8F, 0.8F, 0.8F, 0.8F));
		} else {
			g.drawImage(img, imgX, imgY);
		}

		if (!"".equals(title.getText()) && isHovered()) {
			final int tx = getOX() + getWidth() / 2 - title.getWidth() / 2;
			final int ty = getOY() - 35;
			title.setPosition(tx, ty);
			UIHelper.drawWindow(g, title.getX(), title.getY(),
					title.getWidth() + 5, 25, 3);
			title.setVisible(true);
		} else {
			title.setVisible(false);
		}
	}

	public void setImageRef(String ref) {
		if (ref == null) {
			this.imageRef = DUMMY_IMAGE;
		} else {
			this.imageRef = ref;
		}
	}

	public String getTitle() {
		return title.getText();
	}

	public void setTitle(String text) {
		this.title.setText(FontHelper.firstToUpper(text));
		this.title.setSize(text.length() * 9, 25);
	}

	@Override
	public void bindTo(RootPane pane) {
		pane.add(title);
	}

	private void createTitle() {
		title = new Label();
		title.setTheme("toolTipFont");
	}

	@Override
	public Widget getBindable() {
		return title;
	}

	@Override
	public void show() {
		super.show();
		if (title != null) {
			title.setVisible(true);
		}
	}

	@Override
	public void hide() {
		super.hide();
		if (title != null) {
			title.setVisible(false);
		}
	}
}
